<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*, java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%
    // 数据库连接相关信息
    Connection conn = null;
    CallableStatement stmt = null;
    ResultSet rs = null;
    String url = "jdbc:mysql://localhost:3306/books?serverTimezone=Asia/Shanghai";  // 替换为实际的时区
    String user = "root";  // 替换为实际的数据库用户名
    String password = "123456";  // 替换为实际的数据库密码
    int studentCount = -1;  // 默认值，表示未成功获取数据
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection(url, user, password);
        String sql = "{? = call GetStudentCount()}";
        stmt = conn.prepareCall(sql);
        stmt.registerOutParameter(1, Types.INTEGER);
        stmt.execute();
        studentCount = stmt.getInt(1);
    } catch (SQLException e) {
        studentCount = -1;  // 出现异常时的处理
    } catch (ClassNotFoundException e) {
        studentCount = -1;  // JDBC 驱动类找不到时的处理
    } catch (Exception e) {
        studentCount = -1;  // 捕获其他异常
    } finally {
        // 确保资源关闭
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            // 记录资源关闭错误
        }
    }
%>

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生管理</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="/css/bootstrap.min.css?v=3.3.7" rel="stylesheet">
    <link href="/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
    <link href="/css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>学生管理 <span id="studentCount">（共 <%= studentCount %>人）</span></h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="table_basic.html#">
                            <i class="fa fa-wrench"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-9 m-b-xs">
                            <button class="btn btn-success " type="button" style="background-color: #0e9aef">
                                <a href="/book/studentServlet?action=saveOrUpdatePage" class="btn bth-success" type="button" style="color:#ffffff">
                                    <i class="fa fa-plus"></i>添加
                                </a>
                            </button>
                        </div>
                        <div class="col-sm-3">
                            <div class="input-group">
                                <form action="/book/studentServlet" id="myForm" method="get">
                                    <div class="input-group">
                                        <input type="hidden" name="action" value="list">
                                        <input type="hidden" id="pageNum" name="pageNum" value="${pageUtils.pageNum}">
                                        <input type="hidden" id="pageSize" name="pageSize" value="${pageUtils.pageSize}">
                                        <input type="text" name="key" value="${pageUtils.key}" placeholder="请输入关键词" class="input-sm form-control">
                                        <span class="input-group-btn">
                                        <button type="submit" class="btn btn-sm btn-primary"> 搜索</button>
                                        </span>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                            <tr>
                                <th></th>
                                <th>编号</th>
                                <th>账号</th>
                                <th>学号</th>
                                <th>年龄</th>
                                <th>性别</th>
                                <th>邮箱</th>
                                <th>电话</th>
                                <th>微信</th>
                                <th>地址</th>
                                <th>所属院系</th>
                                <th>所属班级</th>
                                <th>日期</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${requestScope.pageUtils.list}" var="entity">
                                <tr>
                                    <td>
                                        <input type="checkbox" checked class="i-checks" name="input[]">
                                    </td>
                                    <td>${entity.id}</td>
                                    <td>${entity.name}</td>
                                    <td>${entity.stuno}</td>
                                    <td>${entity.age}</td>
                                    <td>${entity.gender}</td>
                                    <td>${entity.email}</td>
                                    <td>${entity.talephone}</td>
                                    <td>${entity.wechat}</td>
                                    <td>${entity.address}</td>
                                    <td>${entity.departname}</td>
                                    <td>${entity.classname}</td>
                                    <td>${entity.createtime}</td>
                                    <td>
                                        <a href="/book/studentServlet?action=saveOrUpdatePage&id=${entity.id}"
                                           class="btn bth-primary" type="button" style="background-color: #0e9aef;color: #ffffff">
                                            <i class="fa fa-edit"></i>更新
                                        </a>
                                        <button class="btn bth-warning " onclick="removeData(${entity.id})"
                                                type="button" style="background-color: #ffaa3c;color: #ffffff">
                                            <i class="fa fa-remove"></i>删除
                                        </button>
                                    </td>
                                </tr>
                            </c:forEach>
                            </tbody>
                        </table>
                    </div>
                    <%@include file="/common/commonPage.jsp"%>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 全局/js -->
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.7"></script>
<script src="/js/plugins/peity/jquery.peity.min.js"></script>
<script src="/js/content.js?v=1.0.0"></script>
<script src="/js/plugins/sweetalert/sweetalert.min.js"></script>
<script src="/js/plugins/iCheck/icheck.min.js"></script>
<script src="/js/demo/peity-demo.js"></script>

<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });

    function removeData(id) {
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            closeOnConfirm: false
        }, function () {


            $.get("/book/studentServlet?action=remove&id=" + id, function (msg) {
                if (msg === "ok"){
                    //表示删除成功重新查询
                    window.location.href = "/book/studentServlet?action=list"
                }else{
                    //表示不能被删除
                    swal("删除失败！", "该学生已经被分配， 不能删除!!!。", "warning");
                }


            })

        });
    }

</script>
</body>
</html>
